from openerp import netsvc
from openerp.osv import fields, osv
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp
import time
from datetime import datetime

class stock_picking(osv.osv):
    _inherit = 'stock.picking'
    _columns = {
        'fleet_to': fields.many2one('fleet.vehicle', 'Fleet Berangkat'),
        'fleet_from': fields.many2one('fleet.vehicle', 'Fleet Kembali'),
        'address_id': fields.char('Alamat Tujuan', size=256),
        'temperatur_bot': fields.float('Temperatur bawah'),        
        'temperatur_up': fields.float('Temperatur atas'),
        }
    
    def action_done(self, cr, uid, ids, context=None):
        picking = self.browse(cr, uid, ids[0])
        fleet_id = self.browse(cr, uid, ids[0]).fleet_from.id      
        if fleet_id:
            fleet_id_state = self.browse(cr, uid, ids[0]).fleet_from.state_id.id
            stated_id = self.pool.get('fleet.vehicle.state').search(cr, uid,[('name','like','On The Way')])
            state_ids = self.pool.get('fleet.vehicle.state').browse(cr, uid, stated_id[0]).id   
        if fleet_id:               
            if fleet_id_state == state_ids:                
                raise osv.except_osv(_('Error!'), _('You Delivery Because the fleet statuss is on the way.'))
            self.pool.get('fleet.vehicle').write(cr, uid, [fleet_id], {'state_id': state_ids, 'picking_id': ids[0],})            
            self.pool.get('fleet.monitor').create(cr, uid, {
                                'ID'      : picking.name,
                                'no_polis': picking.name,
                                'no_so': picking.sale_id and picking.sale_id.id or False,
                                'address_id': picking.address_id,
                                'no_fleet': fleet_id,
                                'picking_id': ids[0],
                                })
        """Changes picking state to done.
        
        This method is called at the end of the workflow by the activity "done".
        @return: True
        """
        self.write(cr, uid, ids, {'state': 'done', 'date_done': time.strftime('%Y-%m-%d %H:%M:%S')})
        return True

stock_picking()

class stock_picking_out(osv.osv):
    _inherit = 'stock.picking.out'
    _columns = {
        'fleet_to': fields.many2one('fleet.vehicle', 'Fleet Berangkat'),
        'fleet_from': fields.many2one('fleet.vehicle', 'Fleet Kembali'),
        'address_id': fields.char('Alamat Tujuan', size=256),
        'temperatur_bot': fields.float('Temperatur bawah'),        
        'temperatur_up': fields.float('Temperatur atas'),
        }
    
    def onchange_partner_in(self, cr, uid, ids, partner_id, context=None):      
        if partner_id:
            partner_id = self.pool.get('res.partner').browse(cr, uid, partner_id).street
        result = {}
        result['address_id'] = partner_id
        
        return {'value': result}

stock_picking_out()